package com.hotel.service;

import com.hotel.entity.Guest;

import com.hotel.repository.GuestRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service("guestService")
public class GuestService {
    private final GuestRepository repository;

    public GuestService() {
        this.repository = new GuestRepository();
    }

    public Guest addGuest(Guest guest) {
        return repository.save(guest);
    }

    public List<Guest> findAll() {
        return repository.findAll();
    }

    public void delete(int id) {
        repository.delete(id);
    }

    public void update(int id,Guest guest) {
        guest.setGuest_id(id);
        repository.update(guest);
    }

    public List<Guest> SearchGuest(String name,String id_card) {
        if(name==null) name="";
        if(id_card==null) id_card="";
        if(!name.isEmpty()||!id_card.isEmpty()){
            return repository.findByNameAndIdCard(name, id_card);
        }
            return repository.findAll();    //可以不用判断，直接返回上面带参数的查询结果
    }
    public Guest findByIdCard(String id_card) {
        return repository.findByIdCard(id_card).
                orElseThrow(() -> new IllegalArgumentException("用户不存在: " + id_card));
    }
}
